//
//  WebCell.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/25.
//

#import "WebCell.h"

@interface WebCell()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, copy) NSString *ishave;

@end

@implementation WebCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _webView.scrollView.scrollEnabled = NO;
    _webView.scalesPageToFit = YES;
    _webView.opaque = NO;
    _webView.backgroundColor = [UIColor clearColor];
    _ishave = @"0";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setViewData:(NSString *)url index:(NSInteger)index {
    _index = index;
    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:encodeUrl]];
    if ([_ishave isEqualToString:@"0"]) {
        [_webView loadRequest:request];
        _ishave = @"1";
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSHTTPURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSLog(@"statusCode:%ld", (long)response.statusCode);
    if (response.statusCode == 404 || response.statusCode == 504 ) {
        _returnBlock(_index);
        return NO;
    }
    return YES;
}

@end
